package com.sumansoul.minamanageconnect;

import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

public class BaseApplication extends Application {

    public static  JWebSocketClient jWebSClient;
    private JWebSocketClientService.JWebSocketClientBinder binder;
    public static  JWebSocketClientService jWebSClientService;
    @Override
    public void onCreate() {
        super.onCreate();
        try {
            startJWebSClientService();
            bindService();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 启动服务（websocket客户端服务）
     */
    private void startJWebSClientService() {
        Intent intent = new Intent(this, JWebSocketClientService.class);
        startService(intent);
    }

    /**
     * 绑定服务
     */
    private void bindService() {
        Intent bindIntent = new Intent(this, JWebSocketClientService.class);
        bindService(bindIntent, serviceConnection, BIND_AUTO_CREATE);
    }


    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.e("MainActivity", "服务与活动成功绑定");
            binder = (JWebSocketClientService.JWebSocketClientBinder) iBinder;
            jWebSClientService = binder.getService();
            jWebSClient = jWebSClientService.client;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e("MainActivity", "服务与活动成功断开");
        }
    };


    public static JWebSocketClient getClient() {
        return jWebSClient;
    }

    public static  JWebSocketClientService getjWebSClientService() {
        return jWebSClientService;
    }
}
